package view;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;

import controller.ImageCoverListener;

import model.MP3FileMutableTreeNode;

/**
 * The GUI class creates the gui of the program.
 * 
 * 
 *
 */
public class GUI extends JFrame {

	private static final long serialVersionUID = 1L;
	
	/*****************************
	 * MEMBER
	 *****************************/
	private DirectoryTree treeDirectoryTree;
	private JScrollPane directoryScrollPane;
	MP3FileMutableTreeNode currentMp3Node;
	
	private JLabel labelTitle;
	private JLabel labelInterpreter;
	private JLabel labelAlbum;
	private JLabel labelYear;
	private JLabel imagePane;
	
	private JTextField textFieldTitle;
	private JTextField textFieldInterpreter;
	private JTextField textFieldAlbum;
	private JTextField textFieldYear;
	
	private JButton buttonSave;
	private JButton buttonDiscard;
	
	private JPopupMenu popup;
	private JMenuItem change;
	private JMenuItem delete;
	
	private Image defaultImage;
	private Image currentImage;
	
	
	/*****************************
	 * CONSTRUCTOR
	 *****************************/
	public GUI() {
		// initialize UI elements
		this.initUserInterfaceElements();

		// initialize layoutManager
		this.initLayout();
		
		// initialize window
		this.initWindow();
		
		// init defaultImage
		try {
			BufferedImage image = ImageIO.read(new File("./bin/resources/nocover.jpg"));
			defaultImage = image.getScaledInstance(this.imagePane.getWidth(), this.imagePane.getHeight(), Image.SCALE_DEFAULT);
			this.setImagePane(defaultImage);
		} catch (IOException ex) {
			System.err.println(ex.getMessage());
			ex.printStackTrace();
		}
	}

	/*****************************
	 * INIT METHODS
	 *****************************/
	private void initUserInterfaceElements() {
	    
	    // initialize labels
		this.labelTitle = new JLabel("Titel");
		this.labelInterpreter = new JLabel("Interpret");
		this.labelAlbum = new JLabel("Album");
		this.labelYear = new JLabel("Jahr");
		this.imagePane = new JLabel("");
		
		// initialize textFields
		this.textFieldTitle = new JTextField(15);
		this.textFieldInterpreter = new JTextField(15);
		this.textFieldAlbum = new JTextField(15);
		this.textFieldYear = new JTextField(15);
		
		// initialize buttons
		this.buttonDiscard = new JButton("Verwerfen");
		this.buttonSave = new JButton("Speichern");

		// initialize image popupmenu
		popup = new JPopupMenu();
		change = new JMenuItem("Coverbild bearbeiten");
		delete = new JMenuItem("Coverbild entfernen");
		
		popup.add(change);
		popup.add(delete);		

	}
	

	public void setDirectoryTree(DirectoryTree directoryTree) {
		// initialize the tree for the filesystem and a testenvironment
	    this.treeDirectoryTree = directoryTree;								
	    directoryScrollPane.setViewportView(treeDirectoryTree);
	    directoryTree.setEditable(true);
	}
	
	private void initWindow(){
		setTitle("MPGI4 Layout Demo");
		setSize(800, 600);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	private void initLayout() {
		//create splitPane with file tree left and other components right
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		splitPane.setResizeWeight(0.20);
		splitPane.setOneTouchExpandable(false);
		this.add(splitPane);
		
		//create a scrollPanel with the file tree
		directoryScrollPane = new JScrollPane();
		directoryScrollPane.setMinimumSize(new Dimension(242, 400));
		splitPane.setLeftComponent(directoryScrollPane);
		
		//create a Panel for the right side of the splitPane
		JPanel rightPanel = new JPanel();
		splitPane.setRightComponent(rightPanel);
		rightPanel.setMinimumSize(new Dimension(540, 400));
		rightPanel.setLayout(null);	
		
		//title
		labelTitle.setBounds(30, 32, 46, 14);
		rightPanel.add(labelTitle);
		textFieldTitle.setBounds(30, 57, 480, 20);
		rightPanel.add(textFieldTitle);

		//interpret
		labelInterpreter.setBounds(30, 88, 100, 14);
		rightPanel.add(labelInterpreter);
		textFieldInterpreter.setBounds(30, 113, 480, 20);
		rightPanel.add(textFieldInterpreter);
		
		//album
		labelAlbum.setBounds(30, 154, 46, 14);
		rightPanel.add(labelAlbum);
		textFieldAlbum.setBounds(30, 179, 210, 20);
		rightPanel.add(textFieldAlbum);
		
		//year
		labelYear.setBounds(300, 154, 46, 14);
		rightPanel.add(labelYear);
		textFieldYear.setBounds(300, 179, 210, 20);
		rightPanel.add(textFieldYear);
		
		//image
		imagePane.setBounds(76, 256, 360, 240);
		rightPanel.add(imagePane);
		
		//saveButton
		buttonSave.setBounds(125, 516, 100, 25);
		rightPanel.add(buttonSave);
		
		//discardButton
		buttonDiscard.setBounds(280, 516, 100, 25);
		rightPanel.add(buttonDiscard);
		
	}
	
	/**
	 * refresh data from mp3 node
	 * @param mp3Node
	 */
	public void redrawGUIWithNewMP3Node(MP3FileMutableTreeNode mp3Node){
		this.currentMp3Node = mp3Node;
		this.textFieldTitle.setText(mp3Node.getMP3File().getTitle());
		this.textFieldAlbum.setText(mp3Node.getMP3File().getAlbum());
		this.textFieldInterpreter.setText(mp3Node.getMP3File().getArtist());
		this.textFieldYear.setText(String.valueOf(mp3Node.getMP3File().getYear()));
		if(mp3Node.getMP3File().getCoverImage() != null) {
			currentImage = mp3Node.getMP3File().getCoverImage().getScaledInstance(this.imagePane.getWidth(), this.imagePane.getHeight(), Image.SCALE_DEFAULT);
		} else {
			currentImage = defaultImage;
		}
		this.imagePane.setIcon(new ImageIcon(currentImage));
	}
	
	public void redrawGUIWithCurrentMP3Node(){
		this.redrawGUIWithNewMP3Node(currentMp3Node);
	}
	
	public void resetGUI() {
		this.labelAlbum.setText("");
		this.labelTitle.setText("");
		this.labelInterpreter.setText("");
		this.labelYear.setText("");
		this.setImagePane();
	}
	
	public MP3FileMutableTreeNode getCurrentMP3Node(){
		return this.currentMp3Node;
	}
	
	public void setCurrentMP3Node(MP3FileMutableTreeNode node) {
		this.currentMp3Node = node;
	}
	
	
	public String getTitle() {
		return this.textFieldTitle.getText();
	}
	
	public String getAlbum() {
		return this.textFieldAlbum.getText();
	}
	
	public String getArtist() {
		return this.textFieldInterpreter.getText();
	}
	
	public String getYear() {
		return this.textFieldYear.getText();
	}
	
	public JButton getSaveButton() {
		return this.buttonSave;
	}
	
	public void setImagePane(Image newImage) {
		currentImage = newImage.getScaledInstance(this.imagePane.getWidth(), this.imagePane.getHeight(), Image.SCALE_DEFAULT);
		this.imagePane.setIcon(new ImageIcon(currentImage));
	}
	public void setImagePane() {
		this.setImagePane(defaultImage);
	}
	public Image getImagePane() {
		return this.currentImage;
	}
	
	public JPopupMenu getPopupMenu() {
		return this.popup;
	}

	/*
	 * action listener adding
	 */
	public void addButtonListener(ActionListener al) {
		this.buttonSave.addActionListener(al);
		this.buttonDiscard.addActionListener(al);
	}


	public void addImageCoverListener(ImageCoverListener ml) {
		this.imagePane.addMouseListener(ml);
		this.change.addActionListener(ml.getAl());
		this.delete.addActionListener(ml.getAl());
		
	}

}